package com.ythook.mentor.service;

import com.ythook.mentor.entity.MentorSkills;
import com.ythook.mentor.entity.Technologies;
import com.ythook.mentor.repository.MentorSkillsRepository;
import com.ythook.mentor.repository.TechnologiesRepository;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import javax.transaction.Transactional;
import java.util.ArrayList;
import java.util.List;
import java.util.Optional;

@Service
@Transactional
public class MentorSkillsService {
    @Resource
    private MentorSkillsRepository mentorSkillsRepository;
    @Resource
    private TechnologiesRepository technologiesRepository;

    public List<MentorSkills> search() {
        return mentorSkillsRepository.findAll();
    }

    public List<Optional<Technologies>> findByMentorId(Long mentorId) {
        List<Optional<Technologies>> list_technologies =  new ArrayList<>();;
        List<MentorSkills> list_mentor_skills = mentorSkillsRepository.findAllByMentorId(mentorId);
        list_mentor_skills.stream().forEach((s)->{
            Optional<Technologies> technologies = technologiesRepository.findById(s.getTechnologyId());
            list_technologies.add(technologies);
        });
        return list_technologies;
    }

    public Optional<MentorSkills> findById(Long id) {
        return mentorSkillsRepository.findById(id);
    }

    public void delete(Long id) {
        mentorSkillsRepository.deleteById(id);
    }

    public void deleteByTechId(Long mentorId, Long techId) {
        mentorSkillsRepository.deleteByTechId(mentorId, techId);
    }

    public MentorSkills add(MentorSkills mentorSkills) {
        return mentorSkillsRepository.save(mentorSkills);
    }

    public void multiAdd(List<MentorSkills> mentorSkillsList) {
        if(mentorSkillsList.size() > 0){
            mentorSkillsRepository.deleteByMentorId(mentorSkillsList.get(0).getMentorId());
            mentorSkillsList.stream().forEach((s)->{
                mentorSkillsRepository.save(s);
            });
        }
    }
//    public MentorSkills update(MentorSkills mentorSkills) {
//            return mentorSkillsRepository.save(mentorSkills);
//    }
}
